#include <math.h>
#include <allegro.h>
#include <iostream>

using namespace std;

BITMAP *buffer;
BITMAP *texture_a;
BITMAP *texture_b;
PALETTE palette;

int build_allegro() {
	if (allegro_init() != 0) {
		cerr << "Cannot initialize allegro resources. Code: " << allegro_error
				<< endl;
		return 1;
	}

	install_timer();
	install_keyboard();

	set_color_depth(32);

	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0) != 0) {
		cerr << "Unable to set any graphic mode. Code: " << allegro_error
				<< endl;
		return 1;
	}

	buffer = create_bitmap(SCREEN_W, SCREEN_H);

	texture_a = load_bitmap(
			"textura-madeira-a.bmp"
			, palette);
	set_palette(palette);

	texture_b = load_bitmap(
			"textura-madeira-b.bmp"
			, palette);
	set_palette(palette);

	return 0;
}

void destroy_allegro() {
	destroy_bitmap(buffer);
	destroy_bitmap(texture_a);
	destroy_bitmap(texture_b);
}

int main(void) {
	if (build_allegro() != 0)
		return 1;

    //x,y,z,u,v,c

	V3D_f vtx_a[4]={
			{250,150,1,  1, 63,0},
			{400,200,1,  1,  1,0},
			{250,250,1, 63,  1,0},
			{100,200,1, 63, 63,0}
	};

	V3D_f vtx_b[4]={
			{400,200,1,  1, 63,0},
			{550,250,1,  1,  1,0},
			{400,300,1, 63,  1,0},
			{250,250,1, 63, 63,0}
	};

	int mode = POLYTYPE_ATEX_MASK;

	while (!key[KEY_ESC]) {
		clear_to_color(buffer, makecol(200,200,200));

		quad3d_f(buffer,mode,texture_a,
				&vtx_a[0],&vtx_a[1],&vtx_a[2],&vtx_a[3]);

		quad3d_f(buffer,mode,texture_b,
				&vtx_b[0],&vtx_b[1],&vtx_b[2],&vtx_b[3]);

		blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
		yield_timeslice();
	}

	destroy_allegro();
	return 0;
}

END_OF_MAIN();
